package game.entities;

import game.gfx.Colours;
import game.gfx.Screen;
import level.Level;

public class Bullet extends Mob
{
	private int colour = Colours.get(-1, -1, -1, 300);
	private int scale = 1;
	
	private int shootingDir;
	private int bulletSpeed = 4;
	
	public static boolean moving;
	public static int bulletReloadTime = 500; // in milli seconds
	public static boolean bulletFired;
	
	public Bullet(Level level, int x, int y)
	{
		super(level, "Bullet", x, y, 1);
		shootingDir = Player.getDir();
	}
	
	public void tick() 
	{
		int xa = 0;
		int ya = 0;
		
		if (shootingDir == 0) 
		{
			ya -= bulletSpeed;
			if (!hasCollided(xa, ya)) moving = true;
			else moving = false;
		}
		if (shootingDir == 1)
		{
			ya += bulletSpeed;
			if (!hasCollided(xa, ya)) moving = true;
			else moving = false;
		}
		if (shootingDir == 2) 
		{
			xa -= bulletSpeed;
			if (!hasCollided(xa, ya)) moving = true;
			else moving = false;
		}
		if (shootingDir == 3)
		{
			xa += bulletSpeed;
			if (!hasCollided(xa, ya)) moving = true;
			else moving = false;
		}
			
		System.out.println(moving);
		
		if (xa != 0 || ya != 0)
		{
			move(xa, ya);
			isMoving = true;
		}
		else isMoving = false;
	}

	public void render(Screen screen) 
	{
		int xTile = 9;
		int yTile = 29;
		int xOffset = x;
		int yOffset = y;
		
		screen.render(xOffset, yOffset, xTile + yTile * 32, colour, flipNone, scale);
	}

	public boolean hasCollided(int xa, int ya) 
	{
		int xMin = 3;
		int xMax = 4;
		int yMin = 3;
		int yMax = 4;
		
		for (int x = xMin; x < xMax; x++)
		{
			if (isSolidTile(xa, ya, x, yMin))
			{
				return true;
			}
		}
		for (int x = xMin; x < xMax; x++)
		{
			if (isSolidTile(xa, ya, x, yMax))
			{
				return true;
			}
		}
		for (int y = yMin; y < yMax; y++)
		{
			if (isSolidTile(xa, ya, xMin, y))
			{
				return true;
			}
		}
		for (int y = yMin; y < yMax; y++)
		{
			if (isSolidTile(xa, ya, xMax, y))
			{
				return true;
			}
		}
		return false;
	}
}
